//
//  RHBatch233VPPConfigVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/2/21.
//

#import "RHBatch233VPPConfigVC.h"
#import "SYChartTool.h"
#import "ZYSwitch.h"
#import "KWTMGRNSettingSyncVC.h"
#import "SettingOptionController.h"
#import "RHDeviceModel.h"
#import "RHEndPointModel.h"
#import "PasswordVerifyController.h"
#import "RHTools.h"
#import "KWT233KSettingDateCell.h"
#import "KWT233KSettingSelectCell.h"
#import "KWT233KInvTimeSetCell.h"
#import "KWT233KSettingEditTimeVC.h"
#import "WSDatePickerView.h"
#import "NetworkAgent.h"
#import "KWTPlantGroupModel.h"
#import "RHUserGroupModel.h"
@interface RHBatch233VPPConfigVC ()
<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray *dataArr;

@property (nonatomic ,strong) NSMutableArray *paraSectionArr;//每组的参数

@property (nonatomic ,assign) NSInteger paraIndex;//参数下标

@property (nonatomic, strong) NSArray *syncArr;//要同步的设备   deviceId

@property (nonatomic ,assign) NSInteger timeIndex;//选择的时间段

@property (nonatomic ,strong) NSArray *timeArr;//对应的6个时间段
@end

@implementation RHBatch233VPPConfigVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"批量策略配置".rh_localized;
    self.paraSectionArr = [NSMutableArray array];
    [self setupUI];
    self.timeIndex = 0;
    [self loadParamData];
}


- (void)setupUI{
    kWeakSelf;
    //233的时间段设置与 S40K的不一样
    NSArray *arr = [SYChartTool loadJsonFileWithName:@"setting_me_233_vpp_set"];
    NSMutableArray *mtbArr = [NSMutableArray arrayWithArray:arr];
    //去掉系统时间校准 和 工作模式
    [mtbArr removeObjectAtIndex:0];
    [mtbArr removeObjectAtIndex:0];
    self.dataArr = [NSArray yy_modelArrayWithClass:[KWT233KSettingModel class] json:mtbArr.copy];
    
    //生成24个时间
    NSMutableArray *rowArr = [NSMutableArray array];
    [self.dataArr enumerateObjectsUsingBlock:^(KWT233KSettingModel*  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([sectionModel.type isEqualToString:@"time"]) {
            KWT233KSettingModel *rowModel = [sectionModel.rows firstObject];
            KWT233KSettingModel *chgTagModel = [rowModel.items safeObjectAtIndex:0];
            KWT233KSettingModel *socModel = [rowModel.items safeObjectAtIndex:1];
            KWT233KSettingModel *powerModel = [rowModel.items safeObjectAtIndex:2];
            for (int i = 0; i <24; i++) {
                KWT233KSettingModel *rowNewModel = [[KWT233KSettingModel alloc]init];
                rowNewModel.type = rowModel.type;
                rowNewModel.comment = [NSString stringWithFormat:@"%02d:00-%02d:00",i,i+1];
                
                NSMutableArray *items = [NSMutableArray array];
                KWT233KSettingModel *chgTagNewModel = [[KWT233KSettingModel alloc]init];
                chgTagNewModel.selectList = chgTagModel.selectList.copy;
                chgTagNewModel.key     = chgTagModel.key;
                chgTagNewModel.format  = chgTagModel.format;
                chgTagNewModel.type    = chgTagModel.type;
                chgTagNewModel.regAddr = chgTagModel.regAddr;
                chgTagNewModel.offset  = chgTagModel.offset;
                chgTagNewModel.num     = chgTagModel.num;
                KWT233KSettingModel *socNewModel = [[KWT233KSettingModel alloc]init];
                socNewModel.key     = [NSString stringWithFormat:@"%@%d",socModel.key,i+1];
                socNewModel.format  = socModel.format;
                socNewModel.type    = socModel.type;
                socNewModel.regAddr = socModel.regAddr + i;
                socNewModel.offset  = socModel.offset;
                socNewModel.num     = socModel.num;
//                socNewModel.content = [NSString stringWithFormat:@"%u",arc4random_uniform(100)];
                KWT233KSettingModel *powerNewModel = [[KWT233KSettingModel alloc]init];
                powerNewModel.key     = [NSString stringWithFormat:@"%@%d",powerModel.key,i+1];
                powerNewModel.format  = powerModel.format;
                powerNewModel.type    = powerModel.type;
                powerNewModel.regAddr = powerModel.regAddr + i;
                powerNewModel.offset  = powerModel.offset;
                powerNewModel.num     = powerModel.num;
//                powerNewModel.content = [NSString stringWithFormat:@"%u",arc4random_uniform(3000)];
                [items addObject:chgTagNewModel];
                [items addObject:socNewModel];
                [items addObject:powerNewModel];
                rowNewModel.items = items.copy;
                [rowArr addObject:rowNewModel];
            }
            sectionModel.rows = rowArr.copy;
        }
    }];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:kCOLOR_HEX(@"#F0F5FF")];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setSeparatorColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[KWT233KSettingDateCell class] forCellReuseIdentifier:@"KWT233KSettingDateCell"];
    [_tableView registerClass:[KWT233KSettingSelectCell class] forCellReuseIdentifier:@"KWT233KSettingSelectCell"];
    [_tableView registerClass:[KWT233KInvTimeSetCell class] forCellReuseIdentifier:@"KWT233KInvTimeSetCell"];
    _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 15)];
    _tableView.tableFooterView = [self footerView];
    [self.view addSubview:_tableView];
   
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(0);
        make.top.mas_offset(0);
    }];
    
}

#pragma mark ---- 获取保存的配置
-(void)loadParamData
{
    [QMUITips showLoadingInView:kAppWindow];
    
    [BaseModel startGetWithCompletionAPIKey:H_getPlantGroupForceConfig argument:@{@"configType":@(2)} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {
            NSDictionary *dataDic = request.responseJSONObject[@"data"];
            if (dataDic[@"strategy"]) {
                NSDictionary *dic = [dataDic[@"strategy"] jsonStringToDictionary];
                NSMutableDictionary *mtbDic = [NSMutableDictionary dictionary];
                //工作模式强制设为削峰填谷 1
                [mtbDic setObject:@"1" forKey:@"EMU_CutPeaks_Fillvalleys"];
                
                if(dic[@"protocolBody"]){
                    NSArray *dicArr = dic[@"protocolBody"][@"commandParamList"];
                    [dicArr enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull paraDic, NSUInteger idx, BOOL * _Nonnull stop) {
                        [mtbDic setObject:ValidNullData(paraDic[@"value"]) forKey:ValidNullData(paraDic[@"paramName"])];
                    }];
                    [self readDefult:mtbDic];
                    [self.tableView reloadData];
                }
                
            }
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
  
    
   
}

-(void)setTimeIndex:(NSInteger)timeIndex
{
    _timeIndex = timeIndex;
    NSMutableArray *arr = [NSMutableArray array];
    [self.dataArr enumerateObjectsUsingBlock:^(KWT233KSettingModel*  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([sectionModel.type isEqualToString:@"time"]) {
            for (int i = 0; i<6; i++) {
                [arr addObject:[sectionModel.rows safeObjectAtIndex:self.timeIndex*6+i]];
            }
        }
    }];
    self.timeArr = arr.copy;
    [self.tableView reloadData];
}



//读取默认值
- (void)readDefult:(NSDictionary *)dic{
    [self.dataArr enumerateObjectsUsingBlock:^(KWT233KSettingModel*  _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
       
        [model readDefult:dic];
        
        //时间校准显示当前时间 ,在分组设置时是读取不到的参数的
        [model.rows enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull row_model, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([row_model.type isEqualToString:@"date"]) {
                row_model.content = [NSDate dateStringWithTimeInterval:[[NSDate date]timeIntervalSince1970] formatter:@"yyyy/MM/dd HH:mm:ss"];
            }
        }];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {//时间段
        return 185;
    }
    return 8;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {//时间段
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 125)];
        headerView.backgroundColor = kCOLOR_HEX(@"#F0F5FF");
        KWT233KSettingModel *sectionModel = self.dataArr[section];
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(15, 15, kUI_WINDOW_W-30, 45)];
        bgView.backgroundColor = UIColor.whiteColor;
        bgView.layer.cornerRadius = 8;
        bgView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMaxXMinYCorner;
        bgView.layer.masksToBounds = YES;
        [headerView addSubview:bgView];
        
        QMUILabel *titleLab = [QMUILabel labelWithText:sectionModel.sectionTitle.rh_localized font:kSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#171A1D")];
        [bgView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.centerY.mas_offset(0);
        }];
        
        
        UIView *line = kView;
        line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
        [bgView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.offset(0);
            make.height.offset(1);
        }];
        
        UIView *timeView = [self createTimeView];
        [headerView addSubview:timeView];
        [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.top.equalTo(line.mas_bottom);
            make.bottom.mas_offset(0);
        }];
        return headerView;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 10;
    }
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    kWeakSelf;
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 10)];
    footerView.backgroundColor = kCOLOR_HEX(@"#F0F5FF");
//    KWTMGRNSettingModel *sectionModel = self.dataArr[section];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, kUI_WINDOW_W-30, 10)];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.maskedCorners = kCALayerMinXMaxYCorner|kCALayerMaxXMaxYCorner;
    bgView.layer.masksToBounds = YES;
    [footerView addSubview:bgView];
    if (section == 0) {
        return footerView;
    }
    
    return footerView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    KWT233KSettingModel *sectionModel = self.dataArr[section];
    if ([sectionModel.type isEqualToString:@"time"]) {
        return self.timeArr.count;
    }
    return sectionModel.rows.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KWT233KSettingModel *sectionModel = self.dataArr[indexPath.section];
    KWT233KSettingModel *rowModel = sectionModel.rows[indexPath.row];
    if ([rowModel.type isEqualToString:@"date"]) {//系统时间校准
        KWT233KSettingDateCell *dateCell = [tableView dequeueReusableCellWithIdentifier:@"KWT233KSettingDateCell"];
        dateCell.model = rowModel;
        kWeakSelf;
        //时间选择
        dateCell.selectAction = ^(KWT233KSettingModel * _Nonnull model) {
            [weakSelf dateAction:model];
        };
        //时间设置
        dateCell.setAction = ^(KWT233KSettingModel * _Nonnull model) {
          
        };
        //时间读取
        dateCell.readAction = ^(KWT233KSettingModel * _Nonnull model) {
//            [weakSelf readParamData];
        };
        [dateCell hideRefreshButton];
        return dateCell;
    }
    if ([rowModel.type isEqualToString:@"select"]) {//工作模式
        KWT233KSettingSelectCell *settingCell = [tableView dequeueReusableCellWithIdentifier:@"KWT233KSettingSelectCell"];
        settingCell.model = rowModel;
        kWeakSelf;
        settingCell.selectAction = ^(KWT233KSettingModel * _Nonnull model) {
            [weakSelf selectAction:model];
        };
       
        return settingCell;
    }else  if ([rowModel.type isEqualToString:@"time"]) {//充放电时段设置
        KWT233KSettingModel *timeRowModel = [self.timeArr safeObjectAtIndex:indexPath.row];
        KWT233KInvTimeSetCell *settingCell = [tableView dequeueReusableCellWithIdentifier:@"KWT233KInvTimeSetCell"];
        timeRowModel.rowTitle = [NSString stringWithFormat:@"%@ %ld",@"时段".rh_localized,indexPath.row+1+self.timeIndex*6];
        settingCell.model = timeRowModel;
        kWeakSelf;
        settingCell.selectAction = ^(KWT233KSettingModel * _Nonnull model) {
            [weakSelf editChargeDischargeInfo:model index:indexPath.row+1+weakSelf.timeIndex*6];
        };
       
        return settingCell;
    }
    return [UITableViewCell new];
}

-(UIView*)createTimeView
{
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;

    __block QMUIButton *lastItem;
    __block CGFloat btnWidth = (kUI_WINDOW_W - 75)/2;
    NSArray *timeArr = @[@"00:00-06:00",@"06:00-12:00",@"12:00-18:00",@"18:00-24:00"];
    [timeArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        QMUIButton *btn = [[QMUIButton alloc]init];
        [btn setTitle:obj forState:UIControlStateNormal];
        btn.titleLabel.font = kSYSTEMFONT(14);
        [btn setTitleColor:[UIColor colorWithHexString:@"3F4443" alpha:0.8] forState:UIControlStateNormal];
        btn.backgroundColor = kCOLOR_HEX(@"F6F8FA");
        btn.layer.cornerRadius = 8;
        btn.userInteractionEnabled = YES;
        [btn setBackgroundColor:kCOLOR_HEX(@"FFFFFF")];
        [bgView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(btnWidth);
            make.height.offset(45);
            if(lastItem){
                if (idx%2==0) {//换行
                    make.left.mas_offset(15);
                    make.top.equalTo(lastItem.mas_bottom).offset(15);
                }else{
                    make.left.equalTo(lastItem.mas_right).offset(15);
                    make.top.equalTo(lastItem);
                }
                
            }else{
                make.left.mas_offset(15);
                make.top.mas_offset(15);
            }
        }];
     
        lastItem = btn;
        kWeakSelf;
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            weakSelf.timeIndex = idx;
//            if (weakSelf.selectTimeBlock) {
//                weakSelf.selectTimeBlock(weakSelf.timeIndex);
//            }
        }];
        
        [RACObserve(self, timeIndex) subscribeNext:^(id  _Nullable x) {
            if([x integerValue] == idx){
                [btn setTitleColor:[UIColor colorWithHexString:@"ffffff" alpha:1] forState:UIControlStateNormal];
                [btn setBackgroundColor:CMainColorGreen];
            }else{
                [btn setTitleColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6] forState:UIControlStateNormal];
                [btn setBackgroundColor:kCOLOR_HEX(@"#F3F7FF")];
            }
        }];
   
    }];
    return bgView;
}

#pragma mark -------- 充放电时间段选择
-(void)editChargeDischargeInfo:(KWT233KSettingModel*)model index:(NSInteger)index
{
    kWeakSelf;
    KWT233KSettingEditTimeVC *editVC = [[KWT233KSettingEditTimeVC alloc]init];
    editVC.index = index;
    editVC.model = model;
    editVC.sureBlock = ^{
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:editVC animated:YES];
}

#pragma mark -------- 选择时间
- (void)dateAction:(KWT233KSettingModel *)settingModel  {
    
    WSDatePickerView *datePickerV = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDayHourMinuteSecond CompleteBlock:^(NSDate *selectDate) {
        NSString *dateFormatStr = @"yyyy/MM/dd HH:mm:ss";
        NSString *currentDate = [selectDate stringWithFormat: dateFormatStr];
        settingModel.content = currentDate;
        [self.tableView reloadData];
    }];
    datePickerV.dateLabelColor = FontColor(@"#3ECE80");//年-月 颜色
    datePickerV.datePickerColor = UIColorBlack;//滚轮日期颜色
    datePickerV.doneButtonColor = UIColor.blackColor;//确定按钮的颜色
    [datePickerV show];
}

#pragma mark ------ 选择
-(void)selectAction:(KWT233KSettingModel*)model
{
    kWeakSelf;
    SettingOptionController *vc = [[SettingOptionController alloc] initWithData:model.selectList];
    [[RHWindowTipManage shareWindowTipManage] showWithController:vc];
    
    vc.curTextValue = [model.content integerValue];
   
    vc.selectBlock = ^(NSInteger index) {
        DevSettingOptionModel *select_model = model.selectList[index];
        model.enumText = select_model.text;//存要显示的内容
        model.content = [NSString stringWithFormat:@"%ld",select_model.textValue];//存下标
        [weakSelf.tableView reloadData];
    };
}


#pragma mark -------- 保存配置

-(void)commitAction
{
    [self createSectionSetPara];
    [self devParamsSetFromNetwork];
}

//把设置指令分组下发   没有值的参数不下发
-(void)createSectionSetPara
{
    self.paraIndex = 0;
    [self.paraSectionArr removeAllObjects];
    
    //工作模式删掉了,要加上
    NSDictionary *modeDic = @{
        @"paramName": @"EMU_CutPeaks_Fillvalleys",
        @"value": @"1",
        @"num": @(16),
        @"offset": @(0),
        @"dataType": @"int",
        @"regAddr": @(1699)
    };
    [self.paraSectionArr addObject:modeDic];
    
    [self.dataArr enumerateObjectsUsingBlock:^(KWT233KSettingModel*  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
       
        [sectionModel.rows enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull rowModel, NSUInteger idx, BOOL * _Nonnull stop) {
            //系统设置不提交(单独提交)
            if ([rowModel.type isEqualToString:@"date"]) {
                return;
            }
            //工作模式
            if ([rowModel.type isEqualToString:@"switch"]||[rowModel.type isEqualToString:@"select"]) {
                if (rowModel.content.length == 0) {
                    return;
                }
                NSMutableDictionary *textDic = [NSMutableDictionary dictionary];
                [textDic setObject:rowModel.format forKey:@"dataType"];
                [textDic setObject:rowModel.key forKey:@"paramName"];
                [textDic setObject:rowModel.content forKey:@"value"];
                [textDic setObject:@(rowModel.regAddr) forKey:@"regAddr"];
                [textDic setObject:@(rowModel.num) forKey:@"num"];
                [textDic setObject:@(rowModel.offset) forKey:@"offset"];
                [self.paraSectionArr addObject:textDic];
            }
            
            //时间段设置
            if ([rowModel.type isEqualToString:@"time"]) {
                KWT233KSettingModel *chgStateModel = [rowModel.items firstObject];//充放电状态
                [rowModel.items enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull itemModel, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (itemModel.content.length == 0||itemModel.key.length == 0) {//充放电字段不提交
                        return;
                    }
                    NSMutableDictionary *itemDic = [NSMutableDictionary dictionary];
                    [itemDic setObject:itemModel.format forKey:@"dataType"];
                    [itemDic setObject:itemModel.key forKey:@"paramName"];
                    [itemDic setObject:itemModel.content forKey:@"value"];
                    [itemDic setObject:@(itemModel.regAddr) forKey:@"regAddr"];
                    [itemDic setObject:@(itemModel.num) forKey:@"num"];
                    [itemDic setObject:@(itemModel.offset) forKey:@"offset"];
                    //功率值要根所充放电状态来发送正负值
                    if (idx == 2) {//功率
                        if ([chgStateModel.content integerValue] == 1) {//充电发负数
                            if (![itemModel.content containsString:@"-"]) {
                                NSString *value = [NSString stringWithFormat:@"-%@",itemModel.content];
                                [itemDic setObject:value forKey:@"value"];
                            }
                        }
                        if ([chgStateModel.content integerValue] == 2) {//放电发正数
                            NSString *value = [itemModel.content stringByReplacingOccurrencesOfString:@"-" withString:@""];
                            [itemDic setObject:value forKey:@"value"];
                        }
                        
                    }
                    
                    //时间
                    if ([itemModel.type isEqualToString:@"date"]) {
                        NSString *content = [itemModel.content stringByReplacingOccurrencesOfString:@":" withString:@""];
                        [itemDic setObject:ValidNullData(content) forKey:@"value"];
                    }
                    [self.paraSectionArr addObject:itemDic];
                }];
            }
            
        }];
        
    }];
    
}

#pragma mark ------ 保存配置
- (void)devParamsSetFromNetwork{

   
    //参数的body
    NSMutableDictionary *bodyDic = [NSMutableDictionary dictionary];
    NSMutableArray *paraListArr = [NSMutableArray array];
    
    //全部参数一次性给到后台 , 不需要填端点sn参数,因为获取不到
    [paraListArr addObjectsFromArray:self.paraSectionArr];

    if (paraListArr.count == 0) {
        return;
    }
    
    //参数的头部
    NSString *randomStr = [NSString generateRandomLetter:15];
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970]*1000;
    NSDictionary *headContent = @{
        @"cmdId": randomStr,
        @"paramCount":@(paraListArr.count),
        @"messageType": @(1),//下行
        @"namespace": @"ParamSetSingle",
        @"payloadVersion": @(1),
        @"timestamp": @((NSInteger)interval)
      };
    
    [bodyDic setObject:paraListArr forKey:@"commandParamList"];
    NSDictionary *para = @{@"protocolHead":headContent,
                           @"protocolBody":bodyDic};
    NSString *paraStr = [para yy_modelToJSONString];
 
    NSDictionary *requestPara = @{
        @"configType":@"2",//1.强制充放电 2.233  3.S40K  4.solark
        @"strategy":paraStr
    };
    
    [QMUITips showLoading:@"保存中...".rh_localized inView:kApplication.keyWindow];

    [BaseModel startWithCompletionAPIKey:H_savePlantGroupForceConfig argument:requestPara requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if (resultModel.code == 200) {

            [QMUITips showSucceed:@"保存完成".rh_localized inView:self.view hideAfterDelay:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
      
    }];
}



-(UIView*)footerView{
    kWeakSelf;
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W, 80)];
    
    QMUIButton *commitBtn = [[QMUIButton alloc]init];
    [commitBtn setTitle:@"保存".rh_localized forState:UIControlStateNormal];
    [commitBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [commitBtn setCornerRadius:8];
    [commitBtn setBackgroundColor:CMainColorGreen];
    [footer addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.height.offset(45);
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    [[commitBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf commitAction];
    }];
    
    return footer;
}




@end
